import type { AddReq } from '@fast-crud/fast-crud';

import type { AxiosRequestConfig, AxiosResponse } from '@vben/request';

import type { DefGenProjectModel } from './model/defGenProjectModel';

import { requestClient } from '#/api/request';
import { ServicePrefixEnum } from '#/enums/commonEnum';
import { RequestEnum } from '#/enums/httpEnum';

const MODULAR = 'defGenProject';
const ServicePrefix = ServicePrefixEnum.GENERATOR;

export const DefGenProjectConfig = {
  Generator: {
    url: `${ServicePrefixEnum.GENERATOR}/${MODULAR}/generator`,
    method: RequestEnum.POST,
  } as AxiosRequestConfig,
};

export namespace DefGenProjectApi {
  export async function getDef() {
    return requestClient.post<DefGenProjectModel.ProjectGeneratorVO>(
      `${ServicePrefix}/${MODULAR}/getDef`,
    );
  }

  export async function generator(
    params: DefGenProjectModel.ProjectGeneratorVO,
  ) {
    return requestClient.post<boolean>(
      `${ServicePrefix}/${MODULAR}/generator`,
      params,
    );
  }

  export const download = (params: DefGenProjectModel.ProjectGeneratorVO) =>
    requestClient.download<AxiosResponse<Blob>>(
      `${ServicePrefix}/${MODULAR}/download`,
      {
        method: RequestEnum.POST,
        params,
        responseReturn: 'raw',
      },
    );

  export const addRequest = async (req: AddReq) => {
    const { form } = req;
    return await generator(form);
  };
}
